package com.luiz.aero.android.computation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DSimple;

import android.os.Environment;

import com.luiz.aero.android.data.Position;
import com.luiz.aero.android.data.Route;

public class ChartGenerator {

	Chart2D chartAltitude;
	Chart2D chartSpeed;
	Chart2D chartOrientation;
	Chart2D chartAcceleration;
	
	String path;


	public ChartGenerator() {
		path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AeroArduino/charts/";
		File folder = new File(path);
		if(!folder.exists()){
			folder.mkdir();
		}
	}
	
	public void generateAllCharts(Route route){
		generateChartAcceleration(route);
		generateChartAltitude(route);
		generateChartOrientation(route);
		generateChartSpeed(route);
		
	}

	public Chart2D generateChartAltitude(Route route){
		int i = -1;
		chartAltitude = new Chart2D();
		ITrace2D traceAltitude = new Trace2DSimple("altitude"); 
		chartAltitude.addTrace(traceAltitude);

		for(Position pos : route.getRoute()){
			i++;
			double altitude = pos.getLocation().getAltitude();
			traceAltitude.addPoint(i,altitude);
		}


		return chartAltitude;
	}

	public Chart2D generateChartSpeed(Route route){
		int i = -1;
		chartSpeed = new Chart2D();
		ITrace2D traceSpeed = new Trace2DSimple("speed"); 
		chartSpeed.addTrace(traceSpeed);

		for(Position pos : route.getRoute()){
			i++;
			double altitude = pos.getLocation().getSpeed();
			traceSpeed.addPoint(i,altitude);
		}


		return chartSpeed;
	}
	
	public Chart2D generateChartOrientation(Route route){
		int i = -1;
		chartOrientation = new Chart2D();
		ITrace2D traceOrientationAzimuth = new Trace2DSimple("azimuth");
		ITrace2D traceOrientationPitch = new Trace2DSimple("pitch");
		ITrace2D traceOrientationRoll = new Trace2DSimple("roll");
		chartOrientation.addTrace(traceOrientationAzimuth);
		chartOrientation.addTrace(traceOrientationPitch);
		chartOrientation.addTrace(traceOrientationRoll);

		for(Position pos : route.getRoute()){
			i++;
			double azimuth = pos.getAzimuth();
			double pitch = pos.getPitch();
			double roll = pos.getRoll();
			traceOrientationAzimuth.addPoint(i,azimuth);
			traceOrientationPitch.addPoint(i,pitch);
			traceOrientationRoll.addPoint(i,roll);
		}
		return chartOrientation;
	}
	
	public Chart2D generateChartAcceleration(Route route){
		int i = -1;
		chartAcceleration = new Chart2D();
		ITrace2D traceAccelerationX = new Trace2DSimple("x");
		ITrace2D traceAccelerationY = new Trace2DSimple("y");
		ITrace2D traceAccelerationZ = new Trace2DSimple("z");
		chartAcceleration.addTrace(traceAccelerationX);
		chartAcceleration.addTrace(traceAccelerationY);
		chartAcceleration.addTrace(traceAccelerationZ);

		for(Position pos : route.getRoute()){
			i++;
			double x = pos.getAccelerationX();
			double y = pos.getAccelerationY();
			double z = pos.getAccelerationZ();
			traceAccelerationX.addPoint(i,x);
			traceAccelerationY.addPoint(i,y);
			traceAccelerationZ.addPoint(i,z);
		}
		return chartAcceleration;
	}

	public void saveObjects(){


		try{
			FileOutputStream f1 = new FileOutputStream(path + "chart_altitude" + new Date().getTime() + ".ser");
			ObjectOutputStream o1 = new ObjectOutputStream(f1);
			o1.writeObject(chartAltitude); // serializo objeto cat
			o1.close();

			FileOutputStream f2 = new FileOutputStream(path + "chart_speed" + new Date().getTime() + ".ser");
			ObjectOutputStream o2 = new ObjectOutputStream(f2);
			o2.writeObject(chartSpeed); // serializo objeto cat
			o2.close();
			
			FileOutputStream f3 = new FileOutputStream(path + "chart_orientation" + new Date().getTime() + ".ser");
			ObjectOutputStream o3 = new ObjectOutputStream(f3);
			o3.writeObject(chartOrientation); // serializo objeto cat
			o3.close();
			
			FileOutputStream f4 = new FileOutputStream(path + "chart_acceleration" + new Date().getTime() + ".ser");
			ObjectOutputStream o4 = new ObjectOutputStream(f4);
			o4.writeObject(chartAcceleration); // serializo objeto cat
			o4.close();
			
			
		}
		catch(Exception e){
			e.printStackTrace();
		}           
	}
	

	/*public void saveImage(){


		try{
			BufferedImage bi = chartAltitude.snapShot();
			ImageIO.write(bi, "JPEG", new File(path + "chartAltitude_"+ new Date().getTime() +".jpg"));

			BufferedImage bi = chartAltitude.snapShot();
			ImageIO.write(bi, "JPEG", new File(path + "chartSpeed_"+ new Date().getTime() +".jpg"));
		}
		catch(Exception e){
			e.printStackTrace();
		}                            


	}*/

}
